package com.ftg.learn.framework.mybatis;
//mybatis io

import com.ftg.learn.framework.mybatis.annotation.Kjx;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * mybatis
 * 1 : xmlbase  (.xml 用java来处理.xml google  mybatis (ibatis louts)  (ssm ssi  ssh) )
 * ... 。。。
 * 2 : annotationbase （还可以与xml混合）
 * 2.1 注解 : @override
 * 3 : javabase
 *
 * 用来完成学生表的信息显示功能
 *
 * @author kangjx
 * @version 1.0
 * @date 2023/8/30 上午8:50
 */

public class Demo01 {

    int a = 9;

    public List<Map<String, Object>> showAll() {
        //总配置文件地址
        String resource = "mybatisconfiguration.xml";

        InputStream inputStream = null;
        try {
            //把mybatisconfiguration里面的信息读取到java里面
            inputStream = Resources.getResourceAsStream(resource);

            //建造者模式  [把xml里面的文本信息读取java并为大量的java类完成初始化赋値]
            SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
            SqlSessionFactory sqlSessionFactory = builder.build(inputStream);

            //打开通道  mybatis 缓存
            SqlSession session = sqlSessionFactory.openSession(true);

            return session.selectList("first.showall");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public List<Map<String, Object>> showWithParameter(String name) {
        //总配置文件地址
        String resource = "mybatisconfiguration.xml";
        InputStream inputStream = null;
        try {
            inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            SqlSession session = sqlSessionFactory.openSession();
            return session.selectList("first.showWithparameter", name);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public int insertStudent(int id, String stu_name, String stu_sex) {
        String resource = "mybatisconfiguration.xml";
        InputStream inputStream = null;
        SqlSession session = null;
        try {
            inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            System.out.println(sqlSessionFactory);
            session = sqlSessionFactory.openSession();
            System.out.println(session);

            // 集合  java
            Map<String,Object> map = new HashMap<>(4);
            map.put("id",id);
            map.put("bbb",stu_name);
            map.put("stu_sex",stu_sex);

            int n = session.insert("first.save", map);
            session.commit();
            return n;
        } catch (IOException e) {
            session.rollback();
        } finally {
            if(null != session)
                session.close();
        }
        return -1;
    }


    public static void main(String[] args) {
        Demo01 demo01 = new Demo01();
        int n = demo01.insertStudent(998,"admin","男");
        System.out.println(n);
//        demo01.showAll().forEach((y)->{
//            y.forEach((k,v)->{
//                System.out.print("key==>"+k+"\t");
//                System.out.print("value==>"+v+"\t");
//            });
//            System.out.println();
//        });
//        demo01.showWithParameter("王").forEach((y) -> {
//            y.forEach((k, v) -> {
//                System.out.print("key==>" + k + "\t");
//                System.out.print("value==>" + v + "\t");
//            });
//            System.out.println();
//        });

    }

    // 前端  vue - java - mybatis - mysql
    // 前端  vue -  go - beego - mysql
    // 前端  vue - c# - mysql
    // 前端  vue - python -jinggo- mysql
    // 端    vue - node .js - mysql

}
